package com.gitee.feizns.explore.data_structure.tree.bs.easy;

/**
 * @author feizns
 * @since 2019/12/25
 */
public class SumOfLeftLeaves {

    public static void main(String[] args) {
        System.out.println(sumOfLeftLeaves(create()));
    }

    private static TreeNode create() {
        TreeNode ret = new TreeNode(1);
        ret.left = new TreeNode(2);
        return ret;
    }

    public static int sumOfLeftLeaves(TreeNode root) {
        return sum(root, null);
    }

    public static int sum(TreeNode curt, TreeNode parent) {
        if ( curt != null ) {
            if ( isLeft(curt, parent) )
                return curt.val;
            return sum(curt.left, curt) + sum(curt.right, curt);
        }
        return 0;
    }

    private static boolean isLeft(TreeNode curt, TreeNode parent) {
        return parent != null && parent.left == curt && curt.left == null && curt.right == null;
    }

}
